#include <iostream>
#include <vector>
#include <fstream>
#include <cctype>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <map>
using namespace std;
//std::string purify(std::string words)
//{
//	std::string purified;
//	for (auto iter = words.begin(); iter != words.end(); ++iter)
//	{
//		if (std::isalpha(*iter))
//		{
//			purified.push_back(*iter);
//		}
//	}
//	return purified;
//}

int readInfo(std::string directory, std::vector<string>& words)
{
	std::ifstream infile(directory);
	if (!infile.good())
	{
		infile.close();
		return -1;
	}
	std::string line;
	std::string temp;

	while (std::getline(infile, line))
	{
		if (line.empty())
		{
			continue;
		}
		else
		{
			std::stringstream ss(line);
			while (ss >> temp)
			{
				words.push_back(temp); 
			}
		}
	}
	return 0;
}
int delInfo(std::vector<string>& words)
{
	int count = 0;
	while (true)
	{
		auto iter = std::find_if(words.begin(), words.end(), // 如果找到s, length > 8 < 3
			[](std::string& s)->bool 
			{
				bool result = false;
				if (s.size() > 8)
				{
					result = true;
				}
				if (s.size() < 3)
				{
					result = true;
				}
				return result;
			});
		if (iter == words.end()) // 如果没找到，（全都erase) 返回 count
		{
			return count;
		}
		else
		{
			words.erase(iter); 
			count++;
		}
	}
}
int delInfo(std::vector<string>& words, char chr)
{
	int count = 0;
	while (true)
	{
		auto iter = std::find_if(words.begin(), words.end(),
			[chr](std::string& s) { return (std::find(s.begin(), s.end(), chr) != s.end()); }); //找到一个单词里有chr
		if (iter == words.end())
		{
			return count;
		}
		else
		{
			words.erase(iter); //擦除
			count++;
		}
	}
}
void showInfo(std::vector<string>& words, int num = 5)
{
	std::vector<string> copy; //拷贝前几个单词
	if (words.size() > 10)
	{
		copy.assign(words.begin(), words.begin() + 10);
	}
	else
	{
		copy.assign(words.begin(), words.end());
	}
	int count = 0; // 计数
	for (auto iter = copy.begin(); iter != copy.end(); ++iter)
	{
		if ((count + 1) % num == 0) // 如果是 num 的倍数， 换行
		{
			std::cout << std::endl;
		}
		std::cout << std::setw(10) << *iter;
		count++;
	}
}

std::vector<std::string> filterInfo(std::vector<std::string>& words)
{
	std::vector<std::string> ret;
	std::for_each(words.begin(), words.end(), // 遍历 words
		[&ret](std::string& s) // 对words里的每个string分析
		{ 
			//if (std::find_if(s.begin(), s.end(), [](char& chr) { return ispunct(chr); }) // 如果有标点
			//	!= s.end())
			auto iter = s.end();
			for (auto i = s.begin(); i != s.end(); ++i)
			{
				if (! ( (*i >= 65 && *i <= 90) || (*i >= 97 && *i <= 122)))
				{
					iter = i;
					break;
				}
			}
			if (iter != s.end())
			{
				ret.push_back(s);//push_back进 ret里
			}
		});
	return ret;
}

void sortInfo(std::vector<std::string>& words)
{
	std::sort(words.begin(), words.end(),
		[](std::string s1, std::string s2) -> bool// s1, s2是深拷贝， 用此来比较
		{
			if (s1.size() < s2.size())
			{
				return true;
			}
			if (s1.size() > s2.size())
			{
				return false;
			}
			std::for_each(s1.begin(), s1.end(), [](char& chr) { chr = std::tolower(chr); });
			// 先全都转化为小写
			return s1 < s2;
		}
		);
}

void writeInfo(std::string directory, std::vector<std::string>& words)
{
	std::ofstream outfile(directory);
	if (!outfile.good()) // 如果流异常 抛出一个异常
	{
		throw "ofstream feels bad now";
	}
	int count = 0; //计数
	for (auto iter = words.begin(); iter != words.end(); ++iter)
	{
		if ((count + 1) % 10 == 0) // 如果是10的倍数， 换行
		{
			outfile << std::endl;
		}
		else
		{
			outfile << *iter << " ";
		}
	}
	outfile.close();
}

char getTopmostChar(std::vector<std::string>& words)
{
	std::map<char, int> record;
	for_each(words.begin(), words.end(), // 对向量遍历
		[&record](std::string s)
		{
			auto temp = record;
			for_each(s.begin(), s.end(), // 对单词遍历
				[&temp](char chr) 
				{
					temp[chr] += 1; //把每个字符存进 record 里
				});
		});
	//auto max_time_iter = std::max(record.begin(), record.end(), // 任意一个出现次数最大的<char, int> 对的迭代器
	//	[](std::pair<char, int>& p)
	//	{
	//		return p.second;
	//	}
	//);
	auto max_pair = std::make_pair<char, int>('a', 0);
	std::vector<std::pair<char, int>> most_appeared;
	std::for_each(record.begin(), record.end(), [&max_pair, &most_appeared](std::pair<const char, int>& p)
		{
			if (p.second > max_pair.second)
			{
				most_appeared.clear();
				max_pair = p;
				most_appeared.push_back(p);
			}
			else if (p.second == max_pair.second)
			{
				most_appeared.push_back(p);
			}
		});
	std::vector<char> get_all_char;
	for (auto iter = most_appeared.begin(); iter != most_appeared.end(); ++iter)
	{
		get_all_char.push_back(iter->first);
	}
	/*
	char ret = get_all_char[0];
	for (auto i = get_all_char.begin(); i != get_all_char.end(); ++i)
	{
		if (*i < ret)
		{
			ret = *i;
		}
	}
	*/ // 编译不通过???
	return '.';
}
int main()
{
	vector<string> words;
	vector<string> specialWords;
	if (readInfo("C:\\data\\info.txt", words) == -1)
	{
		cout << "打开C:\\data\\info.txt文件失败" << endl;
	}
	else
	{
		cout << "读取到" << words.size() << "个单词" << endl;
	}
	cout << "删除了长度大于8和小于3的单词" << delInfo(words) << "个" << endl;
	cout << "删除了包含字符','的单词" << delInfo(words, ',') << "个" << endl;
	cout << "删除后的前10个单词是：\n";
	showInfo(words, 6);
	specialWords = filterInfo(words);
	cout << "\n筛选出" << specialWords.size() << "个单词，前10个单词是：" << endl;
	showInfo(specialWords);

	sortInfo(specialWords);
	cout << "\n排序后的前10个单词是：\n";
	showInfo(specialWords);
	writeInfo("C:\\data\\result.txt", specialWords);
	char ch = getTopmostChar(specialWords);
	cout << "\nspecialWords中出现次数最多的字符是" << ch << endl;
}
